<?php


namespace Frieza\Modules\RiskContent\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\service\Forbid;
use Doraemon\tools\Ip;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Doraemon\service\Themis\Log as ThemisLog;
use Doraemon\model\Themis\Log as ThemisLogModel;
class ForbiddenUserController extends Controller
{
    public function addForbidden(Request $request)
    {
        $code = 1;
        try {
            $requestData = $request->all();
            $adminId = Admin::user()->id;
            $ip = Ip::getClientIp();
            if (empty($requestData['uid'])) {
                throw new \Exception('用户uid不能为空');
            }

            if (empty($requestData['time'])) {
                throw new \Exception('禁言时间不能为空');
            }
            Forbid::setForbid($requestData['uid'], $requestData['time'], $ip,$adminId);
            // 添加themis日志
            $iDay = $requestData['time']/86400;
            $aOpt = array(
                'admin_id'    => $adminId,
                'remark'      => "账号已被禁言{$iDay}天",
                'status'      => ThemisLogModel::STATUS_STOP_SAYING,
                'action_type' => ThemisLogModel::ACTION_TYPE_ADD_STOP_SAYING,
                'illegal_or_punish' => ThemisLogModel::ACTION_PUNISH,
            );
            $aThemisLog = ThemisLog::fetchThemisLogData($requestData['uid'], $aOpt);
            ThemisLog::add($aThemisLog);
            $msg = '禁言成功';
        } catch (\Exception $e) {
            $code = 0;
            $msg = $e->getMessage();
        }
        return compact('code', 'msg');

    }

    public function freeForbid(Request $request)
    {
        $code = 1;
        try {
            $requestData = $request->all();
            $adminId = Admin::user()->id;
            $ip = Ip::getClientIp();
            if (empty($requestData['uid'])) {
                throw new \Exception('用户uid不能为空');
            }
            Forbid::delForbid($requestData['uid'],$adminId,$ip,'解禁');
            $msg = '解禁成功';
            // 添加themis日志
            $aOpt = array(
                'admin_id'    => $adminId,
                'remark'      => "账号已被解除禁言",
                'status'      => ThemisLogModel::STATUS_STOP_SAYING,
                'action_type' => ThemisLogModel::ACTION_TYPE_DEL_STOP_SAYING,
                'illegal_or_punish' => ThemisLogModel::ACTION_PUNISH,
            );
            $aThemisLog = ThemisLog::fetchThemisLogData($requestData['uid'], $aOpt);
            ThemisLog::add($aThemisLog);
        } catch (\Exception $e) {
            $code = 0;
            $msg = $e->getMessage();
        }
        return compact('code', 'msg');
    }

}